package com.tianyu.inspection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.FileTypeEnum;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.enums.Role;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.common.component.Validate;
import com.tianyu.inspection.entity.Danger;
import com.tianyu.inspection.entity.DangerProcess;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IDangerProcessService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 施工单位接口
 * @Date 2021/1/15 - 18:12
 **********************************/

@RestController
@RequestMapping("/api/2.0/dangerProcess")
public class DangerProcessController {

    @Resource
    private IDangerProcessService dangerProcessService;

    @Resource
    private Validate validateModel;

    @GetMapping(value = "/list")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<IPage<DangerProcess>> getPageList(@RequestHeader("u-uid") Integer userId,
                                                @RequestHeader("u-projectId") Integer projectId, HttpServletRequest request) {
        QueryMo queryMo = QueryMo.getQueryMo(request);
        int roleId = (int) request.getAttribute("roleId");
        if (roleId == 4) {
            queryMo.put("inspectorId", userId);
            queryMo.put("roleId", roleId);
        }
        Ro<IPage<DangerProcess>> ro = new Ro<>();
        queryMo.build(DangerProcess.class, projectId);
        return ro.setContent(dangerProcessService.searchListPage(queryMo));
    }

    /**
     * 查询险情处理结果(包括险情责任人)
     *
     * @param id 险情id
     * @return
     */
    @Permission(value = 4, tags = Constants.API_PATROL)
    @GetMapping(value = "/list/danger/{id}")
    public Ro<List<DangerProcess>> dangerProcessList(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        QueryMo queryMo = new QueryMo();
        queryMo.put("dangerId", id);
        queryMo.build(Danger.class, projectId);
        Ro<List<DangerProcess>> ro = new Ro<>(RoState.SUCCESS);
        List<DangerProcess> processPage = dangerProcessService.selectDangerProcessAndInspectorList(queryMo);
        ro.setContent(processPage);
        if (processPage == null || processPage.size() == 0) {
            ro.setMessage("未查询到记录");
        }
        return ro;
    }

    @GetMapping(value = "/admin/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<DangerProcess> getOne(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<DangerProcess> ro = new Ro<>();
        if (Objects.isNull(id)) {
            return ro.setResultEnum(RoState.PARAMS_MISS_ERROR);
        }
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(DangerProcess.class, projectId);
        DangerProcess dangerProcess = dangerProcessService.selectOne(queryMo);
        if (Objects.isNull(dangerProcess)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(dangerProcess);
        }
        return ro;
    }

    @PostMapping(value = "/insert")
    @Permission(value = 3, tags = Constants.API_COMPANY)
    public Ro<DangerProcess> insert(@RequestBody OperateMo operateMo, @RequestHeader("u-projectId") Integer projectId) {
        Ro<DangerProcess> ro = new Ro<>();
        operateMo.createBuild(DangerProcess.class, projectId);
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        Integer projectId1 = (Integer) operateMo.getEntity().get("projectId");
        Integer inspectorId = (Integer) operateMo.getEntity().get("inspectorId");
        Integer dangerId = (Integer) operateMo.getEntity().get("dangerId");
        if (Objects.isNull(projectId1)) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[projectId]");
        } else {
            if (validateModel.projectIdValidate(projectId1)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[projectId],值不存在");
            }
        }
        if (Objects.isNull(inspectorId)) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[inspectorId]");
        } else {
            if (validateModel.inspectorIdValidate(inspectorId, projectId1)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[inspectorId],值不存在或非巡线员ID");
            }
        }
        if (Objects.isNull(operateMo.getEntity().get("dangerId"))) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[dangerId]");
        } else {
            if (validateModel.dangerIdValidate(dangerId, projectId1)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[dangerId],值不存在");
            }
        }
        if (!dangerProcessService.insert(operateMo)) {
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PutMapping(value = "/modify")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<DangerProcess> modify(@RequestBody OperateMo operateMo,
                                    @RequestHeader("u-uid") Integer inspectorId,
                                    @RequestHeader("u-projectId") Integer projectId,
                                    HttpServletRequest request) {
        int roleId = (int) request.getAttribute("roleId");
        Ro<DangerProcess> ro = new Ro<>();
        if (roleId != Role.ROLE_PATROL.getValue()) {
            return ro.setResultEnum(RoState.FAIL).setMessage("巡线员用户接口,其他权限用户无法访问");
        }
        operateMo.put("inspectorId", inspectorId);
        final ServiceRo serviceRo = operateMo.updateBuild(DangerProcess.class, projectId);
        if (!serviceRo.getResult()) {
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        Integer id = (Integer) operateMo.get("id");
        String sceneImage = (String) operateMo.getEntity().get("sceneImage");
        if (Objects.isNull(id)) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        } else {
            if (validateModel.dangerProcessIdValidate(id, projectId)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[id],值不存在");
            }
        }
        if (Objects.isNull(sceneImage)) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[sceneImage]");
        } else {
            if (validateModel.uploadImageValidate(FileTypeEnum.FILE_DANGER_PROCESS_IMG.getValue(), sceneImage)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[sceneImage],文件不存在或文件上传类别错误");
            }
        }
        if (!dangerProcessService.update(operateMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<DangerProcess> delete(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<DangerProcess> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(DangerProcess.class, projectId);
        if (!dangerProcessService.delete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/delete/logical/{id}")
    @Permission(value = 2, tags = Constants.API_ADMIN)
    public Ro<DangerProcess> logicallyDelete(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<DangerProcess> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(DangerProcess.class, projectId);
        if (!dangerProcessService.logicallyDelete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}
